package com.jucai.pppeiyouclient.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.PositiveParticipantAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.ParticipantOrderBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class PositiveEnlistUsersActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "PositiveEnlistUsersActivity : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView partsRv;
    private List<ParticipantOrderBean> participants = new ArrayList<>();
    private PositiveParticipantAdapter partAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_ptr);
        ((TextView) findViewById(R.id.common_title_text)).setText("已报名用户");
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        partAdapter = new PositiveParticipantAdapter(PositiveEnlistUsersActivity.this, participants);
        partsRv = (PullToRefreshForRecyclerView) findViewById(R.id.content_view);

        partsRv.setLayoutManager(new GridLayoutManager(PositiveEnlistUsersActivity.this, 2, LinearLayoutManager.VERTICAL, false));
        partsRv.setItemAnimator(new DefaultItemAnimator());
        partsRv.setAdapter(partAdapter);
        partAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {

            }

            @Override
            public void onItemLongClickListener(View view, int position) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Conductor_Activity.getapplylist");
        param.put("id", getIntent().getStringExtra("activity_id"));
        param.put("page", "1");
        LogUtils.e(TAG + "param = " + param);

        CommonRequestNetData.getJsonArrayData(PositiveEnlistUsersActivity.this, param, true,new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsArr = " + jsonArray);
                participants = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<ParticipantOrderBean>>() {
                }.getType());
                partAdapter.setData(participants);
            }
        });
    }

    public static void starPosEnlist(Context context, String activityId) {
        Intent intent = new Intent(context, PositiveEnlistUsersActivity.class);
        intent.putExtra("activity_id", activityId);
        context.startActivity(intent);
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                startActivity(new Intent(PositiveEnlistUsersActivity.this, MainActivity.class));
                break;
        }
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Conductor_Activity.getapplylist");
                param.put("id", getIntent().getStringExtra("activity_id"));
                param.put("page", page + "");
                LogUtils.e(TAG + "param = " + param);

                CommonRequestNetData.getJsonArrayData(PositiveEnlistUsersActivity.this, param,true, new CommonRequestNetData.JsonArrayCallBack() {
                    @Override
                    public void parseJsonArray(JSONArray jsonArray) {
                        LogUtils.e(TAG + "jsArr = " + jsonArray);
                        List<ParticipantOrderBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<ParticipantOrderBean>>() {
                        }.getType());
//                        participants.addAll(list);
                        partAdapter.setData(participants);
                    }
                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
